import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(executable_path='Chromedriver.exe')
driver.get("https://demo6.tp-shop.cn/")

# 放大浏览器页面
driver.maximize_window()

# # 1). 手动登录TP，获取cookie
# # 1. 登录TP，登录成功后抓取 (PHPSESSID)
# # 2. 使用add_cookie()方法，添加 (PHPSESSID)键和值
driver.add_cookie({"name": "PHPSESSID", "value": "9sp16tiegfc4vvu47v1kio99e3"})
driver.add_cookie({"name": "user_id", "value": "314"})
driver.add_cookie({"name": "uname", "value": "18776895770"})


cookies = driver.get_cookies()
print("cookies内容为:", cookies)
for co in cookies:
    print(co['name'])
# 3. 调用刷新方法 driver.refresh()
driver.refresh()

# 定位搜索框，输入“小米note3”
driver.find_element_by_id("q").send_keys("小米note3")

# 定位搜索按钮并点击，执行搜索操作
driver.find_element_by_css_selector("button").click()

# 定位小米note3商品的“加入购物车”按钮并点击
driver.find_element_by_xpath("/html/body/div[4]/div/div[2]/div[3]/ul/li/div/div[6]/div[2]").click()

# 定位“去购物车结算”按钮，通过JavaScript执行点击（解决元素遮挡等问题）
link = driver.find_element(By.XPATH, "//*[contains(text(), '去购物车结算')]")
driver.execute_script("arguments[0].click();", link)

# 定位“去结算”按钮并点击，进入订单填写页面
driver.find_element_by_css_selector("a.paytotal").click()

# 定位包含收货信息的iframe并切换上下文（操作iframe内元素的前提）
iframe = driver.find_element_by_id("layui-layer-iframe1")
driver.switch_to.frame(iframe)

# 显式等待收货人输入框可点击，输入收货人姓名“刘坤”
wait = WebDriverWait(driver, 10)
consignee_input = wait.until(
    EC.element_to_be_clickable((By.NAME, "consignee"))
)
consignee_input.send_keys("刘坤")

# 定位省下拉框，选择“广西壮族自治区”
province = driver.find_element_by_css_selector("#province")
province_select = Select(province)
province_select.select_by_visible_text("广西壮族自治区")

# 显式等待市下拉框可点击，确保“南宁市”选项加载后选择
city = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "#city"))
)
city_select = Select(city)
WebDriverWait(driver, 10).until(
    lambda d: any("南宁市" in opt.text for opt in city_select.options)
)
city_select.select_by_visible_text("南宁市")

# 显式等待区域下拉框可点击，确保“江南区”选项加载后选择
district = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "#district"))
)
district_select = Select(district)
WebDriverWait(driver, 10).until(
    lambda d: any("江南区" in opt.text for opt in district_select.options)
)
district_select.select_by_visible_text("江南区")

# 显式等待乡镇下拉框可点击，确保“吴圩镇”选项加载后选择
twon = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, "#twon"))
)
twon_select = Select(twon)
WebDriverWait(driver, 10).until(
    lambda d: any("吴圩镇" in opt.text for opt in twon_select.options)
)
twon_select.select_by_visible_text("吴圩镇")

# 显式等待详细地址输入框可点击，输入详细地址“龙江路50号”
address_input = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "address"))
)
address_input.send_keys("龙江路50号")

# 显式等待邮编输入框可点击，输入邮编“530225”
zipcode_input = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "zipcode"))
)
zipcode_input.send_keys("530225")

# 显式等待手机号输入框可点击，输入手机号“18776895770”
mobile_input = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "mobile"))
)
mobile_input.send_keys("18776895770")

# 定位“保存收货地址”按钮并点击（完成收货信息提交）
driver.find_element_by_xpath("/html/body/div/div/div/form/table/tbody/tr[6]/td[2]/button").click()

# 切换回默认页面上下文（若后续需操作iframe外元素）
driver.switch_to.default_content()



